﻿//********************************************************************************//
//创建日期：<创建日期，2016-09-28>
//创建作者：<孙雪婷，sunxueting@shanghai3h.com>
//功能说明：采灌井结账
//********************************************************************************//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entity;
using BLL;
using BLL.Access;
using System.Data;
using Platform.Http;
using Web.Class;

namespace Web.Pages.QuShui.DiXiaS.ShouFeiGL
{
    public partial class CaiGuanJJZ : BasePage
    {
        #region 属性

       
        /// <summary>
        /// 保存结果集
        /// </summary>
        private DataTable dtSource
        {
            get
            {
                return ViewState["dtSource"] as DataTable;
            }
            set
            {
                ViewState["dtSource"] = value;
            }
        }

        #endregion

        #region 变量
        /// <summary>
        /// 存储过程类
        /// </summary>
        SpHelper _sphelper = new SpHelper();

        /// <summary>
        /// 特殊用户
        /// </summary>
        QS_SF_TESHUYH _TSYH = new QS_SF_TESHUYH();

        /// <summary>
        /// 特殊用户BLL
        /// </summary>
        QS_SF_TESHUYHBLL _TSYHBLL = new QS_SF_TESHUYHBLL();
        /// <summary>
        /// 操作日志BLL
        /// </summary>
        SYS_LOGBLL _SYS_LOGBLL = new SYS_LOGBLL();

        QS_YG_YONGHUXXBLL _YHXXBLL = new QS_YG_YONGHUXXBLL();
        #endregion

        #region 事件

        /// <summary>
        /// 页面加载事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)//第一次页面加载
            {
                InitData();
                LoadData();
            }
            //每次回发时从内存中取得数据源重新绑定,否则列头无法排序
            this.gvList.DataSource = this.dtSource;
            this.gvList.DataBind();
        }

        /// <summary>
        ///绑定空表头
        /// </summary>
        /// <param name="sender"></param>
        protected override void Render(HtmlTextWriter sender)
        {
            base.Render(sender);
        }

        /// <summary>
        /// 响应前台回调
        /// </summary>
        protected void ASPxCallback1_Callback(object source, DevExpress.Web.CallbackEventArgs e)
        {
            
            string msg = "";
            if (e.Parameter == "yz")
            {
                string yhh = txtYongHuH.Text;
                DataTable dt = _YHXXBLL.SelectQS_YG_YONGHUXXesDynamic(string.Format("i_jlzt=0 and s_yonghuh='{0}'", yhh), null);
                msg = dt.Rows.Count > 0 ? dt.Rows[0]["S_YONGHUM"].ToString() : "0";
                e.Result = e.Parameter + "|0|" + msg;
            }
            else if (e.Parameter == "info")
            {
                int id = Convert.ToInt32(this.hdCollection.Get("ID"));
                DataTable dt = _sphelper.P_QSSF_TESHUYH(id, Convert.ToInt32(txtShuiLiangN.Text)).Tables[0];
                msg = string.Join("|", dt.Rows[0]["S_YONGHUH"].ToString(), dt.Rows[0]["S_YONGHUM"].ToString(),
                    dt.Rows[0]["LX"].ToString(), dt.Rows[0]["I_SHUILIANG"].ToString());
                e.Result = e.Parameter + "|0|" + msg;
            }
            else if (e.Parameter == "del")
            {
                int id = Convert.ToInt32(this.hdCollection.Get("ID"));
                _TSYH = _TSYHBLL.SelectQS_SF_TESHUYH(id);
                this._TSYH.S_CAOZUOR = LoginInfo.CurrentUser.UserID.ToString();
                this._TSYH.D_CAOZUORQ = BusinessTool.GetDataBaseDateTime();
                this._TSYH.I_JLZT = -1;
                if (_TSYHBLL.InsertOrUpdate(_TSYH, null))
                {
                    _SYS_LOGBLL.Log<QS_SF_TESHUYH>(LoginInfo.CurrentUser, BusinessTool.WType.ToString(), "采灌井结账", OperateState.Delete, _TSYH, null);
                }

                e.Result = e.Parameter + "|0|" + "操作成功！";
            }

            else if (e.Parameter == "save")
            {
                int id = Convert.ToInt32(this.hdCollection.Get("ID"));
                OperateState operateState;
                //添加
                if (id <= 0)
                {
                    operateState = OperateState.Add;
                    _TSYH.ID = 0;
                    _TSYH.I_JLZT = 0;
                }
                //修改
                else
                {
                    //修改操作
                    operateState = OperateState.Modify;
                    _TSYH = _TSYHBLL.SelectQS_SF_TESHUYH(id);
                }
                this._TSYH.S_YONGHUH = txtYongHuH.Text;
                this._TSYH.I_TYPE = Convert.ToInt32(cmbTeShuLX.Value);
                this._TSYH.I_Y = Convert.ToInt32(txtShuiLiangN.Text);
                this._TSYH.I_SHUILIANG = Convert.ToInt32(txtShuiLiang.Text);
                this._TSYH.S_TIANJIAR = LoginInfo.CurrentUser.Account;
                this._TSYH.D_TIANJIARQ = BusinessTool.GetDataBaseDateTime();
                this._TSYH.S_CAOZUOR = LoginInfo.CurrentUser.Account;
                this._TSYH.D_CAOZUORQ = BusinessTool.GetDataBaseDateTime();
                if (_TSYHBLL.InsertOrUpdate(_TSYH, null))
                {
                    _SYS_LOGBLL.Log<QS_SF_TESHUYH>(LoginInfo.CurrentUser, BusinessTool.WType.ToString(), "采灌井结账", operateState, _TSYH, null);
                    e.Result = e.Parameter + "|0|" + "保存成功！";
                }
            }
            else if (e.Parameter == "nextY")
            {
                string msg1 = "";
                _sphelper.P_QSSF_JINRUXYSLNY(3, LoginInfo.CurrentUser.UserID, out msg1);
            }
        }

        /// <summary>
        /// 加载列表
        /// </summary>
        protected void gvList_CustomCallback(object sender, DevExpress.Web.ASPxGridViewCustomCallbackEventArgs e)
        {
            this.LoadData();
        }

        #endregion

        #region 方法
        /// <summary>
        /// 数据加载
        /// </summary>
        private void InitData()
        {

            txtShuiLiangN.Text = BusinessTool.GetSLYMorZWNY(1, 4).ToString();
        }


        /// <summary>
        /// 数据加载
        /// </summary>
        private void LoadData()
        {
            int year = Convert.ToInt32(txtShuiLiangN.Text);
            this.dtSource = _sphelper.P_QSSF_TESHUYH(0, year).Tables[0];
            this.gvList.DataSource = this.dtSource;
            this.gvList.DataBind();
        }

        /// <summary>
        /// 采灌井开账
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnCaiGuanJKZ_Click(object sender, EventArgs e)
        {
            txtCGJJG.Text = "";
            int YM = BusinessTool.GetSLYMorZWNY(1, 2);
            int y = Convert.ToInt32(txtShuiLiangN.Text);
            int user = LoginInfo.CurrentUser.UserID;
            DataTable dt = _sphelper.P_QSSF_CGJKZ(y, user, YM).Tables[0];
            string allmsg = "";
            int con = 0;
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    allmsg = allmsg + dt.Rows[i]["S_YONGHUH"].ToString() + "：开账完成\r\n";
                }

            }
            else
            {
                allmsg = txtShuiLiangN.Text + "年没有采灌结账户，请检查";
            }
            con = dt.Rows.Count > 0 ? dt.Rows.Count : 0;
            txtCGJJG.Text = allmsg + "共" + con.ToString() + "条";
        }

        /// <summary>
        /// 采灌井结账
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnCaiGuanJJZ_Click(object sender, EventArgs e)
        {
            txtCGJJG.Text = "";
            int y = Convert.ToInt32(txtShuiLiangN.Text);
            int user = LoginInfo.CurrentUser.UserID;
            DataTable dt = _sphelper.P_QSSF_CGJJZ(y, user).Tables[0];
            string allmsg = "";
            int con = 0;
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    allmsg = allmsg + dt.Rows[i]["S_YONGHUH"].ToString() + "：结账完成\r\n";
                }

            }
            else
            {
                allmsg = txtShuiLiangN.Text + "年没有采灌结账户，请检查";
            }
            con = dt.Rows.Count > 0 ? dt.Rows.Count : 0;
            txtCGJJG.Text = allmsg + "共" + con.ToString() + "条";
        }

        

        #endregion    

    }
}